Insper 2022-33
Meu nome é Julio. Eu gosto de MasterChef
Eu não gosto de
Meu papel nessa disciplina será ajudar no aprendizado da parte técnica – códigos etc.
Nesse lab, nosso objetivo será construir soluções em R e python para problemas comuns de transformação de dados.
Equipes de 3 pessoas. Começaremos com uma alocação por afinidade. Dependendo dos resultados, realocamos.
Eu serei o Google. Na parte do R, verificarei se vocês fizeram tudo certo. Na parte do python, vocês vão me ensinar.
No final de cada exercício, discutiremos aspectos teóricos sobre as ferramentas (se necessário).
# A tibble: 344 × 8
especie ilha comprimento…¹ profu…² compr…³ massa…⁴ sexo ano
<fct> <fct> <dbl> <dbl> <int> <int> <fct> <int>
1 Pinguim-de-adélia Torgersen 39.1 18.7 181 3750 macho 2007
2 Pinguim-de-adélia Torgersen 39.5 17.4 186 3800 fêmea 2007
3 Pinguim-de-adélia Torgersen 40.3 18 195 3250 fêmea 2007
4 Pinguim-de-adélia Torgersen NA NA NA NA <NA> 2007
5 Pinguim-de-adélia Torgersen 36.7 19.3 193 3450 fêmea 2007
6 Pinguim-de-adélia Torgersen 39.3 20.6 190 3650 macho 2007
7 Pinguim-de-adélia Torgersen 38.9 17.8 181 3625 fêmea 2007
8 Pinguim-de-adélia Torgersen 39.2 19.6 195 4675 macho 2007
9 Pinguim-de-adélia Torgersen 34.1 18.1 193 3475 <NA> 2007
10 Pinguim-de-adélia Torgersen 42 20.2 190 4250 <NA> 2007
# … with 334 more rows, and abbreviated variable names ¹comprimento_bico,
# ²profundidade_bico, ³comprimento_nadadeira, ⁴massa_corporal
# ℹ Use `print(n = ...)` to see more rows
# A tibble: 3 × 4
especie media mediana diferenca
<fct> <dbl> <dbl> <dbl>
1 Pinguim-de-barbicha 48.8 49.6 0.716
2 Pinguim-gentoo 47.5 47.3 0.205
3 Pinguim-de-adélia 38.8 38.8 0.00861
especie media mediana diferenca
0 Pinguim-de-barbicha 48.833824 49.55 0.716176
1 Pinguim-gentoo 47.504878 47.30 0.204878
2 Pinguim-de-adélia 38.791391 38.80 0.008609
# A tibble: 7,240 × 60
pais iso2 iso3 ano novos…¹ novos…² novos…³ novos…⁴ novos…⁵ novos…⁶
<chr> <chr> <chr> <int> <int> <int> <int> <int> <int> <int>
1 Afeganistão AF AFG 1980 NA NA NA NA NA NA
2 Afeganistão AF AFG 1981 NA NA NA NA NA NA
3 Afeganistão AF AFG 1982 NA NA NA NA NA NA
4 Afeganistão AF AFG 1983 NA NA NA NA NA NA
5 Afeganistão AF AFG 1984 NA NA NA NA NA NA
6 Afeganistão AF AFG 1985 NA NA NA NA NA NA
7 Afeganistão AF AFG 1986 NA NA NA NA NA NA
8 Afeganistão AF AFG 1987 NA NA NA NA NA NA
9 Afeganistão AF AFG 1988 NA NA NA NA NA NA
10 Afeganistão AF AFG 1989 NA NA NA NA NA NA
# … with 7,230 more rows, 50 more variables: novos_fpp_h65 <int>,
# novos_fpp_m014 <int>, novos_fpp_m1524 <int>, novos_fpp_m2534 <int>,
# novos_fpp_m3544 <int>, novos_fpp_m4554 <int>, novos_fpp_m5564 <int>,
# novos_fpp_m65 <int>, novos_fpn_h014 <int>, novos_fpn_h1524 <int>,
# novos_fpn_h2534 <int>, novos_fpn_h3544 <int>, novos_fpn_h4554 <int>,
# novos_fpn_h5564 <int>, novos_fpn_h65 <int>, novos_fpn_m014 <int>,
# novos_fpn_m1524 <int>, novos_fpn_m2534 <int>, novos_fpn_m3544 <int>, …
# ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
# A tibble: 3 × 6
pais `2008` `2009` `2010` `2011` `2012`
<chr> <int> <int> <int> <int> <int>
1 Brasil 37697 39212 37874 40253 40108
2 Estados Unidos 4742 4010 3526 3686 3562
3 Índia 615492 624617 630164 642311 629589
pais 2008 2009 2010 2011 2012
0 Brasil 37697 39212 37874 40253 40108
1 Estados Unidos 4742 4010 3526 3686 3562
2 Índia 615492 624617 630164 642311 629589
# A tibble: 26,115 × 15
origem ano mes dia hora tempe…¹ ponto…² umidade direc…³ veloc…⁴ veloc…⁵
<chr> <int> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 EWR 2013 1 1 1 39.0 26.1 59.4 270 10.4 NA
# … with 26,114 more rows, 4 more variables: precipitacao <dbl>, pressao <dbl>,
# visibilidade <dbl>, data_hora <dttm>, and abbreviated variable names
# ¹temperatura, ²ponto_condensacao, ³direcao_vento, ⁴velocidade_vento,
# ⁵velocidade_rajada
# ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
# A tibble: 1,458 × 8
codigo_aeroporto nome latit…¹ longi…² altura fuso_…³ horar…⁴ fuso_…⁵
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 04G Lansdowne Air… 41.1 -80.6 1044 -5 A Americ…
# … with 1,457 more rows, and abbreviated variable names ¹latitude, ²longitude,
# ³fuso_horario, ⁴horario_verao, ⁵fuso_horario_iana
# ℹ Use `print(n = ...)` to see more rows
# A tibble: 16 × 2
companhia_aerea nome
<chr> <chr>
1 9E Endeavor Air Inc.
# … with 15 more rows
# ℹ Use `print(n = ...)` to see more rows
# A tibble: 3,322 × 9
codigo_cauda ano tipo fabri…¹ modelo motores assen…² veloc…³ tipo_…⁴
<chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
1 N10156 2004 Ala fixa mu… EMBRAER EMB-1… 2 55 NA Turbo …
# … with 3,321 more rows, and abbreviated variable names ¹fabricante,
# ²assentos, ³velocidade, ⁴tipo_motor
# ℹ Use `print(n = ...)` to see more rows
# A tibble: 336,776 × 19
ano mes dia horario_sa…¹ saida…² atras…³ horar…⁴ chega…⁵ atras…⁶ compa…⁷
<int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr>
1 2013 1 1 517 515 2 830 819 11 UA
# … with 336,775 more rows, 9 more variables: voo <int>, cauda <chr>,
# origem <chr>, destino <chr>, tempo_voo <dbl>, distancia <dbl>, hora <dbl>,
# minuto <dbl>, data_hora <dttm>, and abbreviated variable names
# ¹horario_saida, ²saida_programada, ³atraso_saida, ⁴horario_chegada,
# ⁵chegada_prevista, ⁶atraso_chegada, ⁷companhia_aerea
# ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
# A tibble: 10 × 5
fabricante nome origem n tempera…¹
<chr> <chr> <chr> <int> <dbl>
1 AIRBUS JetBlue Airways JFK 21003 54.8
2 EMBRAER JetBlue Airways JFK 16785 57.9
3 BOEING Delta Air Lines Inc. JFK 15474 57.2
4 BOMBARDIER INC Endeavor Air Inc. JFK 13844 55.6
5 AIRBUS INDUSTRIE Delta Air Lines Inc. LGA 8233 55.2
6 BOMBARDIER INC ExpressJet Airlines Inc. LGA 7573 60.5
7 MCDONNELL DOUGLAS AIRCRAFT CO Delta Air Lines Inc. LGA 6845 59.3
8 AIRBUS INDUSTRIE US Airways Inc. LGA 6280 57.1
9 BOEING Southwest Airlines Co. LGA 6072 57.3
10 BOEING American Airlines Inc. JFK 5146 56.3
# … with abbreviated variable name ¹temperatura_media
fabricante nome origem n \
0 AIRBUS JetBlue Airways JFK 21003
1 EMBRAER JetBlue Airways JFK 16785
2 BOEING Delta Air Lines Inc. JFK 15474
3 BOMBARDIER INC Endeavor Air Inc. JFK 13844
4 AIRBUS INDUSTRIE Delta Air Lines Inc. LGA 8233
5 BOMBARDIER INC ExpressJet Airlines Inc. LGA 7573
6 MCDONNELL DOUGLAS AIRCRAFT CO Delta Air Lines Inc. LGA 6845
7 AIRBUS INDUSTRIE US Airways Inc. LGA 6280
8 BOEING Southwest Airlines Co. LGA 6072
9 BOEING American Airlines Inc. JFK 5146
temperatura_media
0 54.811566
1 57.870006
2 57.198679
3 55.559239
4 55.164580
5 60.480547
6 59.313049
7 57.140805
8 57.276017
9 56.301159
voos_select <- voos |>
select(
ano, mes, dia, hora,
companhia_aerea, cauda,
origem, destino,
y = atraso_saida
) |>
drop_na(y)
voos_select# A tibble: 328,521 × 9
ano mes dia hora companhia_aerea cauda origem destino y
<int> <int> <int> <dbl> <chr> <chr> <chr> <chr> <dbl>
1 2013 1 1 5 UA N14228 EWR IAH 2
2 2013 1 1 5 UA N24211 LGA IAH 4
3 2013 1 1 5 AA N619AA JFK MIA 2
4 2013 1 1 5 B6 N804JB JFK BQN -1
5 2013 1 1 6 DL N668DN LGA ATL -6
6 2013 1 1 5 UA N39463 EWR ORD -4
7 2013 1 1 6 B6 N516JB EWR FLL -5
8 2013 1 1 6 EV N829AS LGA IAD -3
9 2013 1 1 6 B6 N593JB JFK MCO -3
10 2013 1 1 6 AA N3ALAA LGA ORD -2
# … with 328,511 more rows
# ℹ Use `print(n = ...)` to see more rows
set.seed(1)
split <- rsample::initial_split(voos_select, prop = .8)
treino <- rsample::training(split)
teste <- rsample::testing(split)
feat_eng <- function(dados) {
# ...exercicio...
dados |>
select(-cauda)
}
treino_eng <- feat_eng(treino)
teste_eng <- feat_eng(teste) # cuidado
modelo <- parsnip::rand_forest("regression", trees = 20) |>
parsnip::set_engine("ranger")
fitted <- parsnip::fit(modelo, y ~ ., data = treino_eng)
preds <- predict(fitted, new_data = teste_eng)
result_antes <- yardstick::rmse_vec(teste_eng$y, preds$.pred)Resultado antes:
[1] 36.98851
Resultado depois:
[1] 35.95097
2022